package com.study.test.controller;


import com.study.test.config.KaptchaConfig;
import com.study.test.entity.Student;
import com.study.test.entity.User;
import com.study.test.service.StudentService;
import com.study.test.service.UserService;;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private StudentService studentService;

    @GetMapping("toLogin")
    public  String toLogin(){
        return "/user/login";
    }

    @PostMapping("/login")
    public String login(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "username",required = false) String username,@RequestParam(value = "password",required = false)String password, Model model,@RequestParam(value = "registerCode",required = false) String registerCode) {

        User user1 = userService.login(username, password);
        if (user1 != null) {
            request.getSession().setAttribute("user", user1);
            boolean same = KaptchaConfig.validate(registerCode, request);
            if(same){
                log.info("验证码验证成功");
                return "redirect:/user/findAll";
            }else{
                log.info("验证码验证失败，返回登录页面");
                return "user/login";
            }


        }

        model.addAttribute("msg", "账号或密码错误");

        return "user/login";

    }




    @GetMapping("/findAll")
    public String check(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                        @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                        Model model){
        Integer totalPage = null;
        pageNum = (pageNum-1)*pageSize;
        List<Student> studentList = studentService.findAll(pageNum,pageSize);
        Integer pageTotal = studentService.findTotalRecord();
        model.addAttribute("studentList",studentList);
        //实行分页
        if(pageTotal % pageSize == 0){
            totalPage = pageTotal / pageSize;
        }else{
            totalPage = pageTotal / pageSize + 1;
        }
        model.addAttribute("totalPage",totalPage);
        return "/user/list";
    }

    @GetMapping("/toRegister")
    public String toRegister(User user, Model model){
        model.addAttribute("user", new User());
        return "user/register";
    }

    @PostMapping("/register")
    public String register( User user,Model model){
        log.info("注册的新用户为{}",user.toString());
        boolean flag = userService.register(user);
        if(flag){
            return "user/login";
        }
        model.addAttribute("msg","命名不规范");
        return "user/register";
    }


    @GetMapping("/logout")
    public String eqit(HttpServletRequest request) {
        request.getSession().removeAttribute("user");
        return "index";
    }

}
